package com.ccn.crm.util;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.*;

/**
 * 获取售后工单
 */
public class GetUsedWork {
    private static final Logger LOG = LoggerFactory.getLogger(GetUsedWork.class);
    public static Map<String, Object> getAfterSales(Map<String, Object> token,String id) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/v2/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();
        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
            //设置查询条件
            Map<Object,Object> data=new HashMap<Object,Object>();
            data.put("dataObjectApiName","CasesObj");
                Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
                searchQueryInfo.put("offset",0);
                searchQueryInfo.put("limit",100);
                    //设置过滤添加数组类型
                    List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
                        Map<Object,Object> conditions=new HashMap<Object,Object>();
                            conditions.put("field_name","_id");
                                List<Object>  values=new ArrayList<>();
//                                    long time = new Date().getTime()- 60000*30;
//                                    Date date = new Date(time);
//                                LOG.info("获取项目的时间："+date);
//                                    LOG.info("获取项目的时间戳："+time);
                               // values.add(time);
                               values.add(id);
                            conditions.put("field_values",values);
                            conditions.put("operator","EQ");
                    filters.add(conditions);
        searchQueryInfo.put("filters",filters);


            data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }

    /**
     * 获取售后工时
     * @param token
     * @return
     * @throws ParseException
     */
    public static Map<String, Object> getWorkTime(Map<String, Object> token) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();

        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","object_JmuW2__c");
            Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
            searchQueryInfo.put("offset",0);
            searchQueryInfo.put("limit",100);
            //设置过滤添加数组类型
            List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
            Map<Object,Object> conditions=new HashMap<Object,Object>();

            conditions.put("field_name","field_Vqeky__c");
            List<Object>  values=new ArrayList<>();
            long time = new Date().getTime()- 60000*60*24;
            System.out.println(time);
            values.add(time);
            conditions.put("field_values",values);
            conditions.put("operator","GTE");
            //添加业务类型，售前还是售后
            Map<Object,Object> condition=new HashMap<Object,Object>();
        condition.put("field_name","record_type");
        condition.put("field_values","default__c");
        condition.put("operator","EQ");
        filters.add(conditions);
        filters.add(condition);
        searchQueryInfo.put("filters",filters);


        data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }

    /**
     * 根据工时工单id查询明细
     * @param token
     * @param id
     * @return
     * @throws ParseException
     */
    public static Map<String, Object> getWorkTimeById(Map<String, Object> token,String id) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();

        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","object_JmuW2__c");
        Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
        searchQueryInfo.put("offset",0);
        searchQueryInfo.put("limit",100);
        //设置过滤添加数组类型
        List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
        Map<Object,Object> conditions=new HashMap<Object,Object>();
        conditions.put("field_name","_id");
        List<Object>  values=new ArrayList<>();
        values.add(id);
        conditions.put("field_values",values);
        conditions.put("operator","EQ");
        filters.add(conditions);
        searchQueryInfo.put("filters",filters);


        data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }

    /**
     * 获取售前工时工单
     * @param token
     * @return
     * @throws ParseException
     */
    public static Map<String, Object> getWorkTimeSq(Map<String, Object> token) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();

        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","object_JmuW2__c");
        Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
        searchQueryInfo.put("offset",0);
        searchQueryInfo.put("limit",100);
        //设置过滤添加数组类型
        List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
        Map<Object,Object> conditions=new HashMap<Object,Object>();

            conditions.put("field_name","field_Vqeky__c");
        List<Object>  values=new ArrayList<>();
        long time = new Date().getTime()- 60000*60*24;
//        long time = 1616169600000l;
        values.add(time);
        conditions.put("field_values",values);
        conditions.put("operator","GTE");
        //添加业务类型，售前还是售后
        Map<Object,Object> condition=new HashMap<Object,Object>();
        condition.put("field_name","record_type");
        condition.put("field_values","record_aCvov__c");
        condition.put("operator","EQ");
        filters.add(conditions);
        filters.add(condition);
        searchQueryInfo.put("filters",filters);
        List<Object>  orders=new ArrayList<>();
        Map<Object,Object> order=new HashMap<Object,Object>();
        order.put("fieldName","field_Vqeky__c");
        order.put("isAsc",true);
        orders.add(order);
        searchQueryInfo.put("orders",orders);
        data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }

    /**
     * 获取售前单信息
     * @param token
     * @param id
     * @return
     * @throws ParseException
     */
    public static Map<String, Object> getSqWorkTimeById(Map<String, Object> token,String id) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/custom/data/query";
        Map<String, Object> jsonObject = new HashMap<String, Object>();

        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","object_zd29h__c");
        Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
        searchQueryInfo.put("offset",0);
        searchQueryInfo.put("limit",100);
        //设置过滤添加数组类型
        List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
        Map<Object,Object> conditions=new HashMap<Object,Object>();
        conditions.put("field_name","_id");
        List<Object>  values=new ArrayList<>();
        values.add(id);
        conditions.put("field_values",values);
        conditions.put("operator","EQ");
        filters.add(conditions);
        searchQueryInfo.put("filters",filters);


        data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }


    /**
     * 获取商机信息
     * @param token
     * @param id
     * @return
     * @throws ParseException
     */
    public static Map<String, Object> getOppoInfo(Map<String, Object> token,String id) throws ParseException {
        String url = "https://open.fxiaoke.com/cgi/crm/v2/data/get";
        Map<String, Object> jsonObject = new HashMap<String, Object>();

        //设置访问参数
        String corpAccessToken = (String)token.get("corpAccessToken");
        String corpId = (String)token.get("corpId");
        String currentOpenUserId = (String)token.get("currentOpenUserId");

        jsonObject.put("corpAccessToken",corpAccessToken);
        jsonObject.put("corpId",corpId);
        jsonObject.put("currentOpenUserId",currentOpenUserId);
        //设置查询条件
        Map<Object,Object> data=new HashMap<Object,Object>();
        data.put("dataObjectApiName","OpportunityObj");
        data.put("objectDataId",id);
//        Map<Object,Object> searchQueryInfo=new HashMap<Object,Object>();
//        searchQueryInfo.put("offset",0);
//        searchQueryInfo.put("limit",100);
//        //设置过滤添加数组类型
//        List<Map<Object,Object>> filters=new ArrayList<Map<Object,Object>>();
//        Map<Object,Object> conditions=new HashMap<Object,Object>();
//        conditions.put("field_name","_id");
//        List<Object>  values=new ArrayList<>();
//        values.add(id);
//        conditions.put("field_values",values);
//        conditions.put("operator","EQ");
//        filters.add(conditions);
//        searchQueryInfo.put("filters",filters);
//
//
//        data.put("search_query_info",searchQueryInfo);
        jsonObject.put("data",data);

        //jsonObject.put("data",data);

        //把参数转化为字符串
        String json = JSON.toJSONString(jsonObject);
        //调用工具进行跨域请求获取token的corpID等参数
        String post = HttpUtil.httpProxy(url, json, "post", "application/json");
        //  String str="{\"corpAccessToken\":\"7129AE8DA6F9C5188C40BA920118A0E2\",\"corpId\":\"FSCID_A6D390D2E5FFF878491C1F5E6DA2994B\",\"expiresIn\":2422,\"errorCode\":0,\"errorMessage\":\"success\"}\n";
        //将获取的参数转化成map类型
        HashMap projectInfo = JSON.parseObject(post, HashMap.class);

        return projectInfo;
    }

}
